using System;
using System.Reflection;

namespace SDK.Scripts.Utils
{
	public static class Factory
	{
		public static T Create<T>()
		{
			return Activator.CreateInstance<T>();
		}

		public static T Create<T>(string typeName) where T : class
		{
			return Create<T>(typeName, null);
		}

		public static T Create<T>(string typeName, object[] args) where T : class
		{
			T result = (T)null;
			try
			{
				result = (T)Activator.CreateInstance(Type.GetType(typeName, throwOnError: true), args);
				return result;
			}
			catch (TypeLoadException ex)
			{
				Debuger.LogError("load type error " + typeName + " ==> " + ex.Message);
				return result;
			}
			catch (Exception ex2)
			{
				Debuger.LogError("type error " + ex2.Message);
				return result;
			}
		}

		private static T _Create<T>(string typeName) where T : class
		{
			T result = (T)null;
			Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
			foreach (Assembly assembly in assemblies)
			{
				try
				{
					Type[] types = assembly.GetTypes();
					Type[] array = types;
					foreach (Type type in array)
					{
						if (type.BaseType == typeof(T) && type.Name == typeName)
						{
							result = (T)Activator.CreateInstance(type);
							return result;
						}
					}
				}
				catch
				{
				}
			}
			return result;
		}
	}
}
